from fastapi import APIRouter, Depends, HTTPException, status, Query
from sqlalchemy.orm import Session
from typing import List, Optional

from app.db.database import get_db
from app.core.security import get_current_admin_user
from app.schemas.post import Post, PostBrief, PostUpdate
from app.crud import post as post_crud

router = APIRouter()

@router.get("/", response_model=List[PostBrief])
async def list_posts(
    skip: int = 0,
    limit: int = 100,
    user_id: Optional[int] = None,
    campsite_id: Optional[int] = None,
    status: Optional[str] = None,
    search: Optional[str] = None,
    current_admin: dict = Depends(get_current_admin_user),
    db: Session = Depends(get_db)
):
    """
    管理员获取所有帖子列表
    可以通过用户ID、营地ID、状态、搜索条件过滤
    """
    posts = post_crud.get_posts(
        db, 
        skip=skip, 
        limit=limit, 
        user_id=user_id, 
        campsite_id=campsite_id,
        status=status,
        search=search,
        is_admin=True  # 标记为管理员查询，可以查看未发布的帖子
    )
    return posts

@router.get("/{post_id}", response_model=Post)
async def get_post(
    post_id: int,
    current_admin: dict = Depends(get_current_admin_user),
    db: Session = Depends(get_db)
):
    """管理员获取指定帖子详情"""
    db_post = post_crud.get_post(db, post_id=post_id, is_admin=True)
    if not db_post:
        raise HTTPException(status_code=404, detail="帖子不存在")
    return db_post

@router.put("/{post_id}", response_model=Post)
async def update_post(
    post_id: int,
    post_update: PostUpdate,
    current_admin: dict = Depends(get_current_admin_user),
    db: Session = Depends(get_db)
):
    """管理员更新帖子"""
    updated_post = post_crud.update_post(
        db, 
        post_id=post_id, 
        post_update=post_update,
        is_admin=True  # 管理员可以更新任何帖子
    )
    if not updated_post:
        raise HTTPException(status_code=404, detail="帖子不存在")
    return updated_post

@router.delete("/{post_id}", status_code=status.HTTP_204_NO_CONTENT)
async def delete_post(
    post_id: int,
    current_admin: dict = Depends(get_current_admin_user),
    db: Session = Depends(get_db)
):
    """管理员删除帖子"""
    result = post_crud.delete_post(db, post_id=post_id, is_admin=True)
    if not result:
        raise HTTPException(status_code=404, detail="帖子不存在")
    return {"status": "success", "message": "帖子删除成功"}

@router.patch("/{post_id}/publish", response_model=Post)
async def publish_post(
    post_id: int,
    current_admin: dict = Depends(get_current_admin_user),
    db: Session = Depends(get_db)
):
    """管理员发布帖子"""
    updated_post = post_crud.update_post_status(
        db, 
        post_id=post_id, 
        is_published=True
    )
    if not updated_post:
        raise HTTPException(status_code=404, detail="帖子不存在")
    return updated_post

@router.patch("/{post_id}/unpublish", response_model=Post)
async def unpublish_post(
    post_id: int,
    current_admin: dict = Depends(get_current_admin_user),
    db: Session = Depends(get_db)
):
    """管理员取消发布帖子"""
    updated_post = post_crud.update_post_status(
        db, 
        post_id=post_id, 
        is_published=False
    )
    if not updated_post:
        raise HTTPException(status_code=404, detail="帖子不存在")
    return updated_post

@router.get("/{post_id}/comments", response_model=List)
async def list_comments(
    post_id: int,
    skip: int = 0,
    limit: int = 100,
    current_admin: dict = Depends(get_current_admin_user),
    db: Session = Depends(get_db)
):
    """管理员获取帖子评论列表"""
    # 验证帖子是否存在
    db_post = post_crud.get_post(db, post_id=post_id, is_admin=True)
    if not db_post:
        raise HTTPException(status_code=404, detail="帖子不存在")
    
    comments = post_crud.get_comments(db, post_id=post_id, skip=skip, limit=limit)
    return comments

@router.delete("/{post_id}/comments/{comment_id}", status_code=status.HTTP_204_NO_CONTENT)
async def delete_comment(
    post_id: int,
    comment_id: int,
    current_admin: dict = Depends(get_current_admin_user),
    db: Session = Depends(get_db)
):
    """管理员删除评论"""
    result = post_crud.delete_comment(db, comment_id=comment_id, is_admin=True)
    if not result:
        raise HTTPException(status_code=404, detail="评论不存在")
    return {"status": "success", "message": "评论删除成功"} 